using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace LuaXna
{
    public class Game : Microsoft.Xna.Framework.Game
    {
        string logpath;
        LuaInterface.Lua lua;

        public Game()
        {
            logpath = "log-" + DateTime.Today.ToShortDateString().Replace('/', '-') + ".log";
            System.IO.File.WriteAllText(logpath, "");

            Log("Loading LUA engine...");

            lua = new LuaInterface.Lua();
            lua.RegisterFunction("Log", this, this.GetType().GetMethod("Log"));
            lua.RegisterFunction("LogError", this, this.GetType().GetMethod("LogError"));

            lua.DoString("luanet.load_assembly(\"Microsoft.Xna.Framework\")");
            lua.DoString("luanet.load_assembly(\"Microsoft.Xna.Framework.Graphics\")");
            lua.DoString("luanet.load_assembly(\"Microsoft.Xna.Framework.Game\")");

            Log("LUA engine loaded!");
        }

        protected override void Initialize()
        {
            try
            {
                lua.DoFile("scripts\\initialize.lua");
            }
            catch (Exception e)
            {
                LogError(e.Message);
            }

            base.Initialize();
        }

        protected override void LoadContent()
        {
            try
            {
                lua.DoFile("scripts\\loadcontent.lua");
            }
            catch (Exception e)
            {
                LogError(e.Message);
            }

            base.LoadContent();
        }

        protected override void UnloadContent()
        {
            try
            {
                lua.DoFile("scripts\\unloadcontent.lua");
            }
            catch (Exception e)
            {
                LogError(e.Message);
            }
            
            base.UnloadContent();
        }

        protected override void Update(GameTime gameTime)
        {
            try
            {
                lua["gameTime"] = gameTime;
                lua.DoFile("scripts\\update.lua");
            }
            catch (Exception e)
            {
                LogError(e.Message);
            }

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            try
            {
                lua.DoFile("scripts\\draw.lua");
            }
            catch (Exception e)
            {
                LogError(e.Message);
            }

            base.Draw(gameTime);
        }

        public void Log(string message)
        {
            System.IO.File.AppendAllText(logpath, message + Environment.NewLine);
        }

        public void LogError(string error)
        {
            System.IO.File.AppendAllText(logpath, "[ERROR] " + error + Environment.NewLine);
        }
    }
}
